<?php
if(isset($_GET['filename'])) $filename = $_GET['filename'];

error_reporting(E_ALL);
if (isset($_FILES['image'])) {
	
	$ftmp = $_FILES['image']['tmp_name'];
	$oname = $_FILES['image']['name'];
   $fname = 'MODULES_DIR/postimgs/upload/'.$oname;
	
	$sql->query('INSERT INTO `'.$sql->prefix.'postimgs` (location) VALUES ("/images/'.$misc->fix($oname).'")');
	
	//$sql->query('SELECT image_id FROM `'.$sql->prefix.'postimgs` WHERE location= '/images/".$oname.');
	$image = $sql->fetch_object($sql->query('SELECT image_id FROM `'.$sql->prefix.'postimgs` WHERE location= '/images/".$oname.'));
		
	if(move_uploaded_file($ftmp, $fname)){
?>
<html><head><script>
		var par = window.parent.document;
		var images = par.getElementById('images_container');
		var imgdiv = images.getElementsByTagName('div')[<?=(int)$_POST['imgnum']?>];
		var image = imgdiv.getElementsByTagName('img')[0];
		imgdiv.removeChild(image);
		imgdiv.className = 'uploaded';
		var image_link = par.createElement('a');
		image_link.href = 'MODULES_DIR/postimgs/pages/edit.php?pic=<?=$oname?>';
		image_link.className = 'uploaded';
		image_link.target = '_blank';
		var image_new = par.createElement('img');
		image_new.src = 'MODULES_DIR/postimgs/pages/resize.php?pic=<?=$oname?>';
		image_new.className = 'loaded';
		imgdiv.appendChild(image_link);
		image_link.appendChild(image_new);
		image_label = document.createElement('p');
		image_label.className = 'image_label';
		imageid = document.createTextNode('<Photo <?php echo $image->image_id; ?>>');
		imgdiv.appendChild(image_label);
		image_label.appendChild(imageid);
		</script></head>
		</html>
<?php
		exit();
	}
}
?>
<html><head>
<script language="javascript">
function upload(){
	var par = window.parent.document;

	// hide old iframe
	var iframes = par.getElementsByTagName('iframe');
	var iframe = iframes[iframes.length - 1];
	iframe.className = 'hidden';

	// create new iframe
	var new_iframe = par.createElement('iframe');
	new_iframe.src = 'MODULES_DIR/postimgs/pages/uploader.php<?php if(isset($_GET['filename'])) echo "?filename=".$filename; ?>';
	new_iframe.frameBorder = '0';
	par.getElementById('iframe_container').appendChild(new_iframe);

	// add image progress
	var images = par.getElementById('images_container');
	var new_div = par.createElement('div');
	var new_img = par.createElement('img');
	new_img.src = 'MODULES_DIR/postimgs/locale/indicator.gif';
	new_img.className = 'load';
	new_div.appendChild(new_img);
	images.appendChild(new_div);
	
	// send
	var imgnum = images.getElementsByTagName('div').length - 1;
	document.iform.imgnum.value = imgnum;
	setTimeout(document.iform.submit(),5000);
}
</script>
<form name="iform" action="" method="post" enctype="multipart/form-data">
<input id="file" type="file" name="image" onchange="upload()" />
<input type="hidden" name="imgnum" />
</form>

